package com.china08.yxyapi.service.mg.impl;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;
import com.china08.yxyapi.service.mg.HomeworkTaskReportService;
import com.china08.yxyapi.util.DateAndLocalDateConvertUtils;

@Service
public class HomeworkTaskReportServiceImpl implements HomeworkTaskReportService {

    @Autowired
    private HomeworkTaskReportRepository homeworkTaskReportRepository;

    @Override
    public Page<HomeworkTaskReport> findStuTaskPageByStudentIdAndStartDateAndTitleAndSubjectName(String studentId,
            Date startDate, String title, String subjectName, Pageable pageable) {
        Page<HomeworkTaskReport> page = null;
        // 学科名称数组
        String[] subjectNameArr = new String[] { "语文", "数学", "英语", "物理", "化学", "生物", "政治", "历史", "地理" };
        List<String> subjectNameLst = Arrays.asList(subjectNameArr);
        // 纠正时间误差 eg: 教师端 2017-03-18 12:00:00 发布 学生端传入字符串2017/3/18 转成时间
        // 2017-03-18 00:00:00查询不准确 应该加一天再查询数据 by sbb
        // 只能查当前时间之前的作业，当前时间之后的作业需要过滤掉
        if (DateAndLocalDateConvertUtils.asLocalDate(startDate).plusDays(1).isAfter(LocalDate.now())) {
            startDate = new Date();
        } else {
            startDate = DateAndLocalDateConvertUtils
                    .asDate(DateAndLocalDateConvertUtils.asLocalDate(startDate).plusDays(1));
        }
        // ##
        // 查询开始
        if (StringUtils.contains(subjectName, "全部")) {// 按照全部 和其他查询
            if (StringUtils.isNotBlank(title)) {// 有关键字
                page = homeworkTaskReportRepository.findByStudentIdAndPublishDateLessThanEqualAndTitleLike(studentId,
                        startDate, title, pageable);
            } else {// 无关键字
                page = homeworkTaskReportRepository.findByStudentIdAndPublishDateLessThanEqual(studentId, startDate,
                        pageable);
            }

        } else if (StringUtils.contains(subjectName, "其它")) {
            if (StringUtils.isNotBlank(title)) {// 有关键字
                page = homeworkTaskReportRepository
                        .findByStudentIdAndPublishDateLessThanEqualAndTitleLikeAndSubjectNameNotIn(studentId, startDate,
                                title, subjectNameLst, pageable);
            } else {// 无关键字
                page = homeworkTaskReportRepository.findByStudentIdAndPublishDateLessThanEqualAndSubjectNameNotIn(
                        studentId, startDate, subjectNameLst, pageable);
            }
        } else {// 按照 "语文", "数学", "英语", "物理", "化学", "生物", "政治", "历史", "地理" 学科查询
            if (StringUtils.isNotBlank(title)) {// 有关键字
                page = homeworkTaskReportRepository
                        .findByStudentIdAndPublishDateLessThanEqualAndTitleLikeAndSubjectName(studentId, startDate,
                                title, subjectName, pageable);
            } else {// 无关键字
                page = homeworkTaskReportRepository.findByStudentIdAndPublishDateLessThanEqualAndSubjectName(studentId,
                        startDate, subjectName, pageable);
            }
        }
        return page;
    }
}
